package 栈.练习;
//最小栈
import java.util.Stack;

//入栈操作，栈为空，stack和minStack两个都要放
    //如果小于minStack中的最后一个元素，则入栈
    //stack出栈，minStack也要进行判断，相同则出栈
class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack  = new Stack<>();
    }
    public void push(int val) {
        if(minStack.empty()){
            //为空，都要入栈
            minStack.push(val);
        }else {
            //不为空进行判断
            if(val<=minStack.peek()){
                minStack.push(val);
            }
        }
        stack.push(val);
    }

    public void pop() {
        if(!stack.empty()){
             int x = stack.pop();
             if(x == minStack.peek()){
                 minStack.pop();
             }
        }
    }

    public int top() {
        if(stack.empty()) {
            return -1;
        }
        return stack.peek();

    }

    public int getMin() {
        if(minStack.empty()){
           return -1;
        }
        return minStack.peek();

    }
}